package com.st.controller.headend;

import com.st.dao.IProductCategoryDao;
import com.st.entity.Goods;
import com.st.entity.ProductCategory;
import com.st.entity.User;
import com.st.service.IUserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("user")
public class  UserController {
    @Autowired
    private IProductCategoryDao productCategoryDao;
    @Autowired
    private IUserService userService;
    @Autowired
    private Jedis jedis;

    //主页
    @RequestMapping("main")
    public ModelAndView mainPage(HttpServletRequest req){
        //1.查询商品分类页
        List<ProductCategory> cs =productCategoryDao.query(new ProductCategory());
        List<Goods> newGoodsList=new ArrayList<>();
        //2.查询新品列表
        for (int i=0;i<8;i++){
            //查询指定哈希表的指定字段的值，返回字符串列表
            List<String> vs=jedis.hmget("g"+i,"id","goodsName","imgUrl","goodsPrice","goodsCount");
            if (vs !=null && vs.size() >0 && vs.get(0)!=null){
                Goods g=new Goods();
                g.getValue(vs);
                newGoodsList.add(g);
            }
        }

        //3.计算（猜你喜欢）商品列表
        ModelAndView mv=new ModelAndView();
        mv.addObject("cs",cs);
        mv.addObject("newGoodsList",newGoodsList);
        //获取session中的数据
        HttpSession session=req.getSession();
        Integer userId= (Integer) session.getAttribute("userId");
        String userPhone= (String) session.getAttribute("userPhone");
        mv.addObject("userId",userId);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/main");
        return mv;
    }
    @RequestMapping("add")
    public ModelAndView add(User user, HttpServletRequest req){
        List<User> list2=userService.queryPhone(user.getUserPhone());
        ModelAndView mv = new ModelAndView();
        if (list2==null||list2.size()==0) {
            String pass = user.getUserPass();
            //md5加密和加盐处理
            String md5Pass2 = String.valueOf(DigestUtils.appendMd5DigestAsHex(pass.getBytes(StandardCharsets.UTF_8), new StringBuilder("wzf")));
            String md5Pass = DigestUtils.md5DigestAsHex(md5Pass2.getBytes(StandardCharsets.UTF_8));
            user.setUserPass(md5Pass);
            int count = userService.add(user);

            if (count == 1) {
                HttpSession session = req.getSession();
                session.setAttribute("userPhone", user.getUserPhone());
                //查询用户Id
                List<User> list = userService.query(user);
                Integer userId = list.get(0).getId();
                session.setAttribute("userId", userId);
                //设置过期事件
                session.setMaxInactiveInterval(60 * 10);
                mv.setViewName("redirect:/user/main");
                return mv;
            } else {
                mv.setViewName("redirect:/login1/enroll");
                return mv;
            }
        }
          mv.setViewName("redirect:/login1/enroll");
        return mv;
    }
    @RequestMapping("myAccount")
    public ModelAndView myAccount(User user,HttpServletRequest req){
        ModelAndView mv=new ModelAndView();
        HttpSession session=req.getSession();
        String userPhone= (String) session.getAttribute("userPhone");
        List<User> list=userService.query(user);
        mv.addObject("list",list.get(0));
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/myAccount");
        return mv;
    }
    //打开充值页面
    @RequestMapping("recharge")
    public String recharge(){
        return "/headend/recharge";
    }
    /*充值金额*/
    @RequestMapping("recharge1")
    @ResponseBody
    public String recharge1(BigDecimal amount,HttpServletRequest request){
        HttpSession session=request.getSession();
        //获取用户的手机号
       String userPhone= (String) session.getAttribute("userPhone");
       User user=new User();
       user.setUserMoney(amount);
       user.setUserPhone(userPhone);
       Integer a=userService.updateMoney1(user);
       if (a==1){
           return "1";
       }else {
           return "0";
       }
    }
}
